/*
 * Copyright (c) 2024. hallele, Inc All rights reserved.
 */

package cn.hallele.ms.portalcenter.domain.das;

import cn.hallele.infrastructure.metadata.io.Page;
import cn.hallele.infrastructure.util.ObjectUtil;
import cn.hallele.mc.sdk.usercenter.common.enums.DeptStatusEnum;
import cn.hallele.mc.sdk.usercenter.dto.DeptDTO;
import cn.hallele.mc.sdk.usercenter.dto.DeptQueryDTO;
import cn.hallele.ms.infrastructure.core.convertor.das.ModelDas;
import cn.hallele.ms.portalcenter.domain.domain.DeptDomain;
import cn.hallele.ms.portalcenter.domain.domain.DeptQueryDomain;
import cn.hallele.ms.portalcenter.integration.action.dept.DeptIntegrationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * hallele_ms_portalcenter
 *
 * @author anle5
 * @since 2024/11/16 18:41
 */
@Slf4j
@Component
public class DeptDDDas extends ModelDas<DeptIntegrationService, DeptDomain, DeptDTO> {
    protected DeptDDDas(DeptIntegrationService option) {
        super(option);
    }

    /**
     * 分页查询部门信息
     *
     * @param condition 查询条件
     * @return 部门信息
     */
    public Page<DeptDomain> findDeptList(DeptQueryDomain condition) {
        return revertPage(this.option.findDeptList(convertQuery(condition)));
    }

    /**
     * 查询单个部门信息
     *
     * @param condition 查询条件
     * @return 部门信息
     */
    public DeptDomain findDept(DeptQueryDomain condition) {
        return revert(this.option.findDept(convertQuery(condition)));
    }

    /**
     * 新增 部门
     *
     * @param deptDomain 部门
     */
    public void addDept(DeptDomain deptDomain) {
        this.option.addDept(convert(deptDomain));
    }

    /**
     * 更新 部门
     *
     * @param deptDomain 部门
     */
    public void updateDept(DeptDomain deptDomain) {
        this.option.updateDept(convert(deptDomain));
    }

    /**
     * 删除 部门
     *
     * @param id 要删除的dept id
     */
    public void deleteDept(Integer id) {
        this.option.deleteDept(id);
    }

    /**
     * 更新 部门 状态
     *
     * @param id     主键
     * @param status 状态
     */
    public void updateDeptStatus(Integer id, DeptStatusEnum status) {
        this.option.updateDeptStatus(id, status);
    }

    /**
     * 将T转成E
     *
     * @param deptDomain 被转换者
     * @return S 转换后者
     */
    private DeptQueryDTO convertQuery(DeptQueryDomain deptDomain) {
        return ObjectUtil.map(deptDomain, d -> DeptQueryDTO.builder().keywords(d.getKeywords()).startTime(d.getStartTime()).endTime(d.getEndTime()).pageNo(d.getPageNo()).pageSize(d.getPageSize()).id(d.getId()).name(d.getName()).phone(d.getPhone()).status(d.getStatus()).build());
    }

    /**
     * 将T转成E
     *
     * @param deptDomain 被转换者
     * @return S 转换后者
     */
    @Override
    protected DeptDTO convert(DeptDomain deptDomain) {
        return ObjectUtil.map(deptDomain, d -> DeptDTO.builder().id(d.getId()).number(d.getNumber()).name(d.getName()).description(d.getDescription()).leader(ObjectUtil.map(d.getLeader(), l -> DeptDTO.Leader.builder().id(l.getId()).username(l.getUsername()).build())).phone(d.getPhone()).email(d.getEmail()).status(d.getStatus()).sorted(d.getSorted()).parentId(d.getParentId()).createTime(d.getCreateTime()).updateTime(d.getUpdateTime()).createBy(d.getCreateBy()).updateBy(d.getUpdateBy()).build());
    }

    /**
     * 将E回转陈T
     *
     * @param deptDTO 被回转换者
     * @return S 回转换后者
     */
    @Override
    protected DeptDomain revert(DeptDTO deptDTO) {
        return ObjectUtil.map(deptDTO, d -> DeptDomain.builder().id(d.getId()).number(d.getNumber()).name(d.getName()).description(d.getDescription()).leader(ObjectUtil.map(d.getLeader(), l -> DeptDomain.Leader.builder().id(l.getId()).username(l.getUsername()).build())).phone(d.getPhone()).email(d.getEmail()).status(d.getStatus()).sorted(d.getSorted()).parentId(d.getParentId()).createTime(d.getCreateTime()).updateTime(d.getUpdateTime()).createBy(d.getCreateBy()).updateBy(d.getUpdateBy()).build());
    }
}
